﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataLayer;
using DevExpress.XtraEditors;

namespace OrderDocument
{
    public partial class frmListe : XtraForm
    {
        public frmListe()
        {
            InitializeComponent();
        }

        public enum ListeTipleri : byte
        {
            CariKodu = 0,
            CariAdi = 1,
            StokKodu = 2,
            StokAdi = 3,
            Barkod = 4,
            SupplierAdi = 5,
            Cekilisler = 6
        }

        public ListeTipleri ListeTipi { get; set; }
        public string Filtre { get; set; }
        public object Value { get; set; }

        void ShowList()
        {
            dbListe liste = new dbListe();
            switch (ListeTipi)
            {
                case ListeTipleri.CariKodu:
                    this.Text = "Cari Listesi";
                    gridControl1.DataSource = liste.CariListeKod(this.Filtre);
                    break;
                case ListeTipleri.CariAdi:
                    this.Text = "Cari Listesi";
                    gridControl1.DataSource = liste.CariListeAd(this.Filtre);
                    break;
                case ListeTipleri.StokKodu:
                    this.Text = "Ürün Listesi";
                    gridControl1.DataSource = liste.StokListeKod(this.Filtre, false);
                    break;
                case ListeTipleri.StokAdi:
                    this.Text = "Ürün Listesi";
                    gridControl1.DataSource = liste.StokListeAd(this.Filtre, false);
                    break;
                case ListeTipleri.Barkod:
                    this.Text = "Ürün Listesi";
                    gridControl1.DataSource = liste.StokListeBarkod(this.Filtre, false);
                    break;
                case ListeTipleri.SupplierAdi:
                    this.Text = "Cari Listesi";
                    gridControl1.DataSource = liste.SupplierListeAd(this.Filtre);
                    break;
                case ListeTipleri.Cekilisler:
                    this.Text = "Çekiliş Listesi";
                    gridControl1.DataSource = liste.CekilisListesi();
                    break;
                default:
                    break;
            }
            gridView1.BestFitColumns();
            gridView1.Focus();
        }

        private void frmListe_Load(object sender, EventArgs e)
        {
            this.ShowList();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
            {
                List<List<string>> rows = new List<List<string>>();
                List<string> cols = new List<string>();

                foreach (int i in gridView1.GetSelectedRows())
                {
                    foreach (DevExpress.XtraGrid.Columns.GridColumn col in this.gridView1.Columns)
                        cols.Add(gridView1.GetRowCellValue(i, col).ToString());
                    rows.Add(cols);
                }

                this.Value = rows;
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                List<List<string>> rows = new List<List<string>>();
                List<string> cols = new List<string>();

                foreach (int i in gridView1.GetSelectedRows())
                {
                    foreach (DevExpress.XtraGrid.Columns.GridColumn col in this.gridView1.Columns)
                        cols.Add(gridView1.GetRowCellValue(i, col).ToString());
                    rows.Add(cols);
                }

                this.Value = rows;
                DialogResult = DialogResult.OK;
                this.Close();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                DialogResult = DialogResult.Abort;
                this.Close();
            }
        }

        private void frmListe_Activated(object sender, EventArgs e)
        {
            gridView1.Focus();
        }

    }
}
